Precedence of Operator

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.The acronym PEMDAS is a useful way to remember the rules:

  • Parentheses have the highest precedence
    • 2 * (3-1) is 4, and
    • (1+1)**(5-2) is 8.
  • Exponentiation has the next highest precedence
    • 2**1+1 is 3, not 4, and
    • 3*1**3 is 3, not 27.
  • Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence.
    • 2*3-1 is 5, not4, and
    • 6+4/2 is 8, not 5.
  • Operators with the same precedence are evaluated from left to right (except exponentiation.So in the expression
    • 4 / 2 * pi, the division happens first and the result is multiplied by pi.

The following table lists all operators from highest precedence to the lowest.

S.NoOperator & Description
1** Exponentiation raisetothepower
2~ +- Complement, unary plus and minus
3* / % //
4+ - Addition and subtraction
5>> << Right and left bitwise shift
6& Bitwise 'AND'
7^ | Bitwise exclusive OR' and regular OR'
8<= >= Comparison operators
9== != Equality operators
10= %= /= //= -= += *= **= Assignment operators
11is is not Identity operators
12in not in Membership operators
13not or and Logical operators

Example: Program link

# Precedence of Operator
# Right-left associativity of ** exponent operator
# Output: 512
print(2 ** 3 ** 2)
# Shows the right-left associativity of **
# Output: 64
print((2 ** 3) ** 2)
# Left-right associativity
# Output: 3
print(5 * 2 // 3)
# Shows left-right associativity
# Output: 0
print(5 * (2 // 3))

References

  • Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
  • Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
  • John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
  • Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
  • Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
  • Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
  • Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.